import pandas as pd
import numpy as np
from osgeo import gdal
gdal.AllRegister()
# 读取TIFF遥感影像


def read_img(filename):
    dataset = gdal.Open(filename)
    ncols = dataset.RasterXSize  # 栅格矩阵的列数
    nrows = dataset.RasterYSize  # 栅格矩阵的行数
    proj = dataset.GetProjection()  # 地图投影信息，字符串表示
    data = dataset.ReadAsArray(0, 0)
    datatype = data.dtype
    adfGeoTransform = dataset.GetGeoTransform()
    # DEM的平面四至
    Xmin = adfGeoTransform[0]
    Ymin = adfGeoTransform[3]
    Xmax = adfGeoTransform[0] + nrows * adfGeoTransform[1] \
        + ncols * adfGeoTransform[2]
    Ymax = adfGeoTransform[3] + nrows * adfGeoTransform[4] \
        + ncols * adfGeoTransform[5]
    xcellwidth = abs(adfGeoTransform[1])
    ycellwidth = abs(adfGeoTransform[5])
    del dataset  # 关闭对象dataset，释放内存
    return {"nrows": nrows, "ncols": ncols, "data": data,
            "Xmin": Xmin, "Ymin": Ymin, "Xmax": Xmax, "Ymax": Ymax,
            "xcellwidth": xcellwidth, "ycellwidth": ycellwidth,
            "proj": proj, "datatype": datatype}
